<?php
	header('content-type:text/html;charset=utf-8'); 
	
	//decodeURIComponent後端解碼
	// $DT=$_GET["DT"];//日期2018-3-10 09:43:05注釋:這個已經取消,自動生成今天的即可
	// echo $DT."</br>";
	$GX=$_GET["GongXu"];//工序
	$DT=date("Y-m-d");//"2018-03-01";//date("Y-m-d")
	
	$BB=$_GET["BanBie"];//班別
	
	if ($BB === "B" && date("G")<20){
		$DT=date("Y-m-d",strtotime("-1 day"));
	}else{
		$DT=date("Y-m-d");
	}
	
	$ZB=$_GET["ZhiBiao"];//製表人
	/*
	echo $GX."</br>";
	echo $DT."</br>";
	echo $BB."</br>";
	echo $ZB."</br>";
	*/
	//从数据库中获取数据，为了节省内存，不要把数据一次性读到内存，从句柄中一行一行读即可 
	
	$conn=mysql_connect("172.31.3.21:3306","root","tdled2018");
	if (!$conn){
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("tddb", $conn);

	mysql_query("set character set 'utf8'");//读库 

	mysql_query("set names 'utf8'");//写库 
	
	//`GongXu`,`BanBie`,
	//原語句備份
	//$sql = "SELECT `JiTai`, `GongDan`, `ChanPinLeiXing`, `ChanPinXiLie`,`ChanPinXingHao`, `JiHuaQTY`, `JiHuaTime` FROM `m_pl_temp` WHERE 1";//`GongXu` = ".$GX." and `RiQi` =".$DT

	$sql = 
	"SELECT 
		`DID`, 
		`d_d_temp`.`HID`, 
		`DingDanHao`, 
		`ChanPinXingHao`, 
		`ChanPinLeiXing`, 
		`ChanPinXiLie`, 
		`GongDanHao`, 
		`JiTai`, 
		`JiHuaQTY`, 
		`JiHuaTime`, 
		`CaoZuoYuan`, 
		`ShiJiQTY`, 
		`ShiJiTime`, 
		`UPH`, 
		`DaiJiTime`, 
		`DianJianTime`, 
		`HuanDanTime`, 
		`HuanHaoCaiTime`, 
		`QingYeJuTime`, 
		`ShouJianTime`, 
		`TiaoJiTime`, 
		`GongChengShiYangiTime`, 
		`GuZhangTime`, 
		`PinZhiYiChangTime`, 
		`DaiLiaoTime`, 
		`ShuiDianQiYiChangiTime`, 
		`QiTaJiHuaWaiTingJiTime`, 
		`WeiDaChengYuanYinBeiZhu`
	FROM 
		`d_h_temp`,`d_d_temp` 
	WHERE `d_h_temp`.`HID` = `d_d_temp`.`HID` and `RiQi` ='".$DT."' and `GongXu` = '".$GX."' and `BanBie` ='".$BB."' and `ZhiBiao` ='".$ZB."'"
	;
	
	//"WHERE `RiQi` ='".$DT."'";//`GongXu` = '".$GX."' and `BanBie` ='".$BB."' and `RiQi` ='".$DT."'";
	
	//echo $sql."</br>";

	$result = mysql_query($sql); 

	$dbdata = array();

	//Fetch into associative array
	while ( $row = mysql_fetch_array($result))  {
		//echo json_encode($row)."</br>";
		$dbdata[]=$row;
	}
	
	//Print array in JSON format
	echo json_encode($dbdata);
	
?>
